package com.better.backend.mqReceiverService.UserInfo.listener;

import com.alibaba.fastjson.JSONObject;
import com.better.backend.mqReceiverService.UserInfo.model.UserModel;
import com.better.backend.mqReceiverService.UserInfo.repository.UserInfoRepsitory;
import com.better.backend.mqReceiverService.examReceiver.model.ExamModel;
import com.better.common.mq.MqTopic;
import com.better.common.mq.configuration.RabbitMqConfiguration;
import com.better.common.mq.struct.MQMessage;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.naming.NameAlreadyBoundException;

/**
 *
 */
@Component
public class UserInfoMessageListener {
    /**
     * logger
     */
    private static final Logger logger = LoggerFactory.getLogger(UserInfoMessageListener.class);
    @Resource
    private UserInfoRepsitory userInfoRepsitory;

    // 一个queue的一种消息,只有一个可以接收
    @RabbitListener(queues = MqTopic.ZXY_ADDUSER) //启用Rabbit队列监听common
    @RabbitHandler
    public void process(@Payload Message message){
        logger.info("接收到新增用户信息: " + message.getBody());
        UserModel userModel = JSONObject.parseObject(message.getBody(), UserModel.class);
        userModel.setRoles(userModel.getUserType());
        userInfoRepsitory.save(userModel);
        logger.info("保存用户信息成功");
    }
}
